cap log close
set more off

* stata-mp8-5g -b do bk-2000-sim2a.do;

log using bk-2000-sim2a.log, replace

*** Primary data pull
use ind1990 classwkr if classwkr==1 using ./data-raw/2000-WorkerResults.dta, clear
gen n=_n
keep n ind1990
sort n
save ./temp-sim2/baseline-set, replace
su

keep if n==1
gen trial=0
gen OVER1=.
gen OVER1sd=.
keep trial OVER1*
save ./temp-sim2/trials, replace

*** Randomizations;
log off

forvalues j = 1/10000 {

use if n<=`j' using ./temp-sim2/baseline-set, replace
save ./temp-sim2/baseline-set-working, replace
collapse (count) n, by(ind1990) fast
sort ind1990
merge ind1990 using ./data-raw/2000-Ind-Distribution-SelfOnly, keep(ctsh) nok
egen emp=total(n)
gen temp1=(n/emp)*(n/emp/ctsh) 
egen OVER1=total(temp1)
keep OVER1
duplicates drop
gen n=0
compress
save ./temp-sim2/trial`j', replace

forvalues i = 1/100 {
set seed `i'
use ./temp-sim2/baseline-set-working, clear
g distr = uniform()
replace ind1990=.
replace ind1990=10 if distr>0
replace ind1990=11 if distr>0.0616348
replace ind1990=20 if distr>0.1045798
replace ind1990=30 if distr>0.1327723
replace ind1990=40 if distr>0.1376375
replace ind1990=60 if distr>0.1409927
replace ind1990=100 if distr>0.3762805
replace ind1990=132 if distr>0.379103
replace ind1990=150 if distr>0.3797946
replace ind1990=160 if distr>0.3815388
replace ind1990=170 if distr>0.3820457
replace ind1990=180 if distr>0.3910416
replace ind1990=200 if distr>0.3920576
replace ind1990=210 if distr>0.3922487
replace ind1990=220 if distr>0.3933206
replace ind1990=230 if distr>0.3936621
replace ind1990=242 if distr>0.4051045
replace ind1990=250 if distr>0.4102705
replace ind1990=270 if distr>0.4123411
replace ind1990=310 if distr>0.4162354
replace ind1990=340 if distr>0.4233969
replace ind1990=350 if distr>0.4253666
replace ind1990=370 if distr>0.4274974
replace ind1990=390 if distr>0.429523
replace ind1990=391 if distr>0.4304359
replace ind1990=392 if distr>0.4330586
replace ind1990=401 if distr>0.4358102
replace ind1990=402 if distr>0.4366308
replace ind1990=410 if distr>0.4427633
replace ind1990=411 if distr>0.4797412
replace ind1990=420 if distr>0.4805552
replace ind1990=440 if distr>0.487648
replace ind1990=471 if distr>0.4897358
replace ind1990=500 if distr>0.4922597
replace ind1990=501 if distr>0.4949426
replace ind1990=502 if distr>0.4957567
replace ind1990=510 if distr>0.4969295
replace ind1990=511 if distr>0.5026539
replace ind1990=512 if distr>0.5030727
replace ind1990=521 if distr>0.5049565
replace ind1990=531 if distr>0.5060026
replace ind1990=532 if distr>0.508026
replace ind1990=540 if distr>0.5101955
replace ind1990=541 if distr>0.5106552
replace ind1990=542 if distr>0.5112845
replace ind1990=550 if distr>0.5124273
replace ind1990=551 if distr>0.5169724
replace ind1990=552 if distr>0.5176211
replace ind1990=560 if distr>0.5186779
replace ind1990=561 if distr>0.5190023
replace ind1990=562 if distr>0.5195414
replace ind1990=571 if distr>0.5213543
replace ind1990=580 if distr>0.5274181
replace ind1990=581 if distr>0.5313511
replace ind1990=582 if distr>0.5335442
replace ind1990=591 if distr>0.5362293
replace ind1990=601 if distr>0.537561
replace ind1990=610 if distr>0.5459532
replace ind1990=611 if distr>0.5470014
replace ind1990=612 if distr>0.5492375
replace ind1990=620 if distr>0.5574107
replace ind1990=621 if distr>0.5613458
replace ind1990=622 if distr>0.5642928
replace ind1990=623 if distr>0.566254
replace ind1990=630 if distr>0.5685373
replace ind1990=631 if distr>0.5689518
replace ind1990=633 if distr>0.5746827
replace ind1990=641 if distr>0.580435
replace ind1990=642 if distr>0.6058953
replace ind1990=650 if distr>0.6078607
replace ind1990=651 if distr>0.6093901
replace ind1990=652 if distr>0.6128097
replace ind1990=660 if distr>0.614395
replace ind1990=662 if distr>0.6166719
replace ind1990=663 if distr>0.6171144
replace ind1990=670 if distr>0.6190218
replace ind1990=671 if distr>0.6199755
replace ind1990=672 if distr>0.6219195
replace ind1990=681 if distr>0.6227593
replace ind1990=682 if distr>0.6241341
replace ind1990=691 if distr>0.6358729
replace ind1990=700 if distr>0.6435284
replace ind1990=702 if distr>0.6442308
replace ind1990=710 if distr>0.6481466
replace ind1990=711 if distr>0.6604739
replace ind1990=712 if distr>0.6778964
replace ind1990=721 if distr>0.7081317
replace ind1990=722 if distr>0.7134394
replace ind1990=731 if distr>0.7263188
replace ind1990=732 if distr>0.7283444
replace ind1990=740 if distr>0.740878
replace ind1990=741 if distr>0.74439
replace ind1990=750 if distr>0.7568011
replace ind1990=751 if distr>0.7596086
replace ind1990=752 if distr>0.7972136
replace ind1990=760 if distr>0.8007363
replace ind1990=761 if distr>0.8149067
replace ind1990=762 if distr>0.816973
replace ind1990=770 if distr>0.8200576
replace ind1990=771 if distr>0.8207342
replace ind1990=772 if distr>0.8238896
replace ind1990=780 if distr>0.8280009
replace ind1990=781 if distr>0.8331347
replace ind1990=791 if distr>0.8348745
replace ind1990=800 if distr>0.8435224
replace ind1990=810 if distr>0.8476788
replace ind1990=812 if distr>0.8533517
replace ind1990=820 if distr>0.8701984
replace ind1990=821 if distr>0.8805969
replace ind1990=822 if distr>0.8838253
replace ind1990=831 if distr>0.8852236
replace ind1990=832 if distr>0.8880805
replace ind1990=840 if distr>0.8884585
replace ind1990=841 if distr>0.8956501
replace ind1990=842 if distr>0.924893
replace ind1990=850 if distr>0.9264116
replace ind1990=860 if distr>0.9273911
replace ind1990=862 if distr>0.9310792
replace ind1990=870 if distr>0.9322757
replace ind1990=871 if distr>0.9327139
replace ind1990=872 if distr>0.9334635
replace ind1990=882 if distr>0.9337105
replace ind1990=890 if distr>0.9483126
replace ind1990=891 if distr>0.9609321
replace ind1990=892 if distr>0.962337
replace ind1990=893 if distr>0.9831318

collapse (count) n, by(ind1990) fast
sort ind1990
merge ind1990 using ./data-raw/2000-Ind-Distribution-SelfOnly, keep(ctsh) nok
egen emp=total(n)
gen temp1=(n/emp)*(n/emp/ctsh) 
egen OVER1=total(temp1)
keep OVER1
duplicates drop
gen n=`i'
compress
append using ./temp-sim2/trial`j'.dta
save ./temp-sim2/trial`j', replace
}

gen trial=`j'
gen OVER1sd=OVER1
collapse (mean) OVER1 (sd) OVER1sd, by(trial)
append using ./temp-sim2/trials.dta
save ./temp-sim2/trials, replace
erase ./temp-sim2/trial`j'.dta
}

log on
su

log close

